package com.hducms.cms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hducms.cms.dao.ContentTxtDao;
import com.hducms.cms.entity.main.Content;
import com.hducms.cms.entity.main.ContentTxt;
import com.hducms.cms.service.ContentTxtService;


@Service
@Transactional
public class ContentTxtServiceImpl implements ContentTxtService {
	public ContentTxt save(ContentTxt txt, Content content) {
		if (txt.isAllBlank()) {
			return null;
		} else {
			txt.setContent(content);
			txt.init();
			dao.save(txt);
			content.setContentTxt(txt);
			return txt;
		}
	}

	public void update(ContentTxt txt, Content content) {
//		ContentTxt entity = dao.findById(content.getId());
//		if (entity == null) {
//			entity = save(txt, content);
//			content.getContentTxtSet().add(entity);
//			return entity;
//		} else {
//			if (txt.isAllBlank()) {
//				content.getContentTxtSet().clear();
//				return null;
//			} else {
//				Updater<ContentTxt> updater = new Updater<ContentTxt>(txt);
//				entity = dao.updateByUpdater(updater);
//				entity.blankToNull();
//				return entity;
//			}
//		}
		Integer id = content.getId();
		if (id!=null) {
			txt.setId(id);
			dao.update(txt);
		}
		
	}

	private ContentTxtDao dao;

	@Autowired
	public void setDao(ContentTxtDao dao) {
		this.dao = dao;
	}
}